package com.service.sys;

import com.common.ErrorCodeEnum;
import com.model.base.BaseEntity;
import com.common.emums.DataStatus;
import com.common.exception.BusinessException;
import com.common.utils.StringUtils;
import com.core.springSecurity.common.SecurityUtils;
import com.dao.sys.SysLoginInfoMapper;
import com.github.pagehelper.PageHelper;
import com.model.pojo.sys.SysLoginInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * @Author: zql
 * @Description: 登录日志服务
 * @Data: 2021-07-14
 */
@Slf4j
@Service
public class SysLoginInfoService {

    @Resource
    SysLoginInfoMapper sysLoginInfoMapper;

    /**
     * @Author: zql
     * @Description: 创建登录日志
     * @param sysLoginInfo 被创建对象主体
     * @Data: 2021-07-14
     */
    public void create(SysLoginInfo sysLoginInfo,String name) {
        sysLoginInfo.setCreateTime(null);
        sysLoginInfo.setUpdateTime(null);
        sysLoginInfo.setCreator(name);
        sysLoginInfo.setModifier(name);
        sysLoginInfoMapper.insertSelective(sysLoginInfo);
    }

    /**
     * @Author: zql
     * @Description: 创建登录日志列表
     * @param sysLoginInfoList 增加的对象主体列表
     * @Data: 2021-07-14
     */
    public void createList(List<SysLoginInfo> sysLoginInfoList) {
        String name = SecurityUtils.getUsername();
        sysLoginInfoList.forEach(o -> {
            o.setCreator(name);
            o.setModifier(name);
            o.setUpdateTime(null);
            o.setCreateTime(null);
        });
        sysLoginInfoMapper.insertListSelective(sysLoginInfoList);
    }

    /**
     * @Author: zql
     * @Description: 更新登录日志信息
     * @param sysLoginInfo 更新的对象主体内容
     * @Data: 2021-07-14
     */
    public void update(SysLoginInfo sysLoginInfo) {
        String name = SecurityUtils.getUsername();
        sysLoginInfo.setCreateTime(null);
        sysLoginInfo.setUpdateTime(null);
        sysLoginInfo.setCreator(null);
        sysLoginInfo.setModifier(name);
        sysLoginInfoMapper.updateByPrimaryKeySelective(sysLoginInfo);
    }

    /**
     * @Author: zql
     * @Description: 更新登录日志列表信息
     * @param sysLoginInfoList 更新的对象主体内容列表
     * @Data: 2021-07-14
     */
    public void updateList(List<SysLoginInfo> sysLoginInfoList) {
        String name = SecurityUtils.getUsername();
        sysLoginInfoList.forEach(o -> {
            o.setCreator(name);
            o.setModifier(name);
            o.setUpdateTime(null);
            o.setCreateTime(null);
        });
        sysLoginInfoMapper.updateListSelective(sysLoginInfoList);
    }

    /**
     * @Author: zql
     * @Description: 更新或创建
     * @param sysLoginInfoList 更新或创建的对象主体内容列表
     * @Data: 2021-07-14
     */
    public void updateAndInsert(List<SysLoginInfo> sysLoginInfoList) {
        List<SysLoginInfo> updateList = new ArrayList<>();
        List<SysLoginInfo> createList = new ArrayList<>();
        for (SysLoginInfo sysLoginInfo : sysLoginInfoList){
            if (StringUtils.isNotNull(sysLoginInfo.getSysLoginInfoId()) &&
                    sysLoginInfoMapper.has(sysLoginInfo.getSysLoginInfoId(), null) >0){
                updateList.add(sysLoginInfo);
            } else {
                createList.add(sysLoginInfo);
            }

        }
        if (updateList.size() > 0) {
            updateList(updateList);
        }
        if (createList.size() > 0) {
            createList(createList);
        }
    }

    /**
     * @Author: zql
     * @Description: 查看登录日志列表
     * @param baseEntity 基础类
     * @Data: 15:02 2021/6/3
     */
    public List<SysLoginInfo> getList(BaseEntity baseEntity) {
        PageHelper.clearPage();
        PageHelper.startPage(baseEntity.getPage(), baseEntity.getPageSize(), baseEntity.getSortRow() + baseEntity.getSortOrder());
        return sysLoginInfoMapper.selectList(baseEntity.getIsDelete());
    }

    /**
     * @Author: zql
     * @Description: 查看登录日志
     * @param sysLoginInfoId 登录日志Id
     * @Data: 2021-07-14
     */
    public SysLoginInfo get(Long sysLoginInfoId, Byte isDelete) {
        SysLoginInfo sysLoginInfo = sysLoginInfoMapper.selectByPrimaryKey(sysLoginInfoId, isDelete);
        if (StringUtils.isNull(sysLoginInfo)) {
            throw new BusinessException(ErrorCodeEnum.DATA_NOT_FOUNT);
        }
        return sysLoginInfo;
    }

    /**
     * @Author: zql
     * @Description: 对象校验
     * @param sysLoginInfo 校验的对象
     * @param toUpdate 确定对象是更新还是创建
     * @Data: 2021-07-14
     */
    public void verifyInfo(SysLoginInfo sysLoginInfo, boolean toUpdate) {
       // boolean toInsert = !toUpdate;
        if (StringUtils.isNotNull(sysLoginInfo.getIsDelete())) {
            if (!DataStatus.contains(sysLoginInfo.getIsDelete())) {
                log.error("数据状态设置错误:{}", sysLoginInfo.getIsDelete());
                throw new BusinessException(ErrorCodeEnum.USER_VERIFY.setParam("数据状态设置错误"));
            }
        }
        if (toUpdate && (StringUtils.isNull(sysLoginInfo.getSysLoginInfoId()) || sysLoginInfoMapper.has(sysLoginInfo.getSysLoginInfoId(), null) == 0)){
            throw new BusinessException(ErrorCodeEnum.DATA_NOT_FOUNT);
        }
    }

    public boolean has(Long sysLoginInfoId,Byte isDelete){
        return sysLoginInfoMapper.has(sysLoginInfoId, isDelete) > 0;
    }
}
